package com.winning.sx.microframework.controller.sysmanage;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.model.exception.CustomException;
import com.winning.sx.microframework.model.manage.RoleManageModel;
import com.winning.sx.microframework.model.message.AjaxDataResponseMessage;
import com.winning.sx.microframework.model.message.AjaxTableResponseMessage;
import com.winning.sx.microframework.service.sysmanage.RoleManageService;
import com.winning.sx.microframework.common.util.BaseUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by MJ on 2016-10-12.
 */
@Controller
@RequestMapping("/rolemanage")
public class RoleManageController {
    @Autowired
    private RoleManageService roleManageService;
    private ObjectMapper objectMapper = new ObjectMapper();

    @ResponseBody
    @RequestMapping(value = {"/", ""})
    public ModelAndView baseroleDef() throws CustomException {
        return new ModelAndView("manage/rolemanage");
    }

    @ResponseBody
    @RequestMapping(value = {"/queryAll"})
    public AjaxTableResponseMessage queryRole() throws CustomException {
        AjaxTableResponseMessage ajaxTableResponseMessage = new AjaxTableResponseMessage();
        try {
            DataTable roleInfos = roleManageService.queryRoles();

            ajaxTableResponseMessage.setDatas(roleInfos.getDataRows());
            ajaxTableResponseMessage.setTotalSize((int) roleInfos.getTotalRowNum());
            ajaxTableResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxTableResponseMessage.setSuccess(false);
            ajaxTableResponseMessage.setErrorMsg(e.getMessage());
        }
        return ajaxTableResponseMessage;
    }

    @ResponseBody
    @RequestMapping("/new")
    public AjaxDataResponseMessage newRole() {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        String cRoleID = BaseUtil.createUUID();
        ajaxDataResponseMessage.setData(cRoleID);
        ajaxDataResponseMessage.setSuccess(true);

        return ajaxDataResponseMessage;
    }

    @ResponseBody
    @RequestMapping("/eidt")
    public AjaxDataResponseMessage roleOperation(HttpServletRequest request) throws CustomException {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String[]> inmap = request.getParameterMap();
        Map<String, Object> map = new HashMap<String, Object>();
        RoleManageModel roleModel = new RoleManageModel();
        try {
            for (String key : inmap.keySet()) {
                if (!key.startsWith("data")) {
                    continue;
                }
                int firstPosition = key.indexOf("]");
                String id = key.substring(5, firstPosition);
                roleModel.setcRoleID(id);
                String field = key.substring(firstPosition + 2, key.length() - 1);
                map.put(field, inmap.get(key)[0]);
            }
            BeanUtils.populate(roleModel, map);
            int result = roleManageService.editRole(roleModel);
            if (result > 0) {
                if ("1".equals(roleModel.getbState())) {
                    roleModel.setbState("启用");
                } else {
                    roleModel.setbState("停用");
                }
                List<RoleManageModel> list = new ArrayList<RoleManageModel>();
                list.add(roleModel);
                ajaxDataResponseMessage.setData(list);
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("角色信息操作失败");
        }
        return ajaxDataResponseMessage;
    }

    @ResponseBody
    @RequestMapping("/del")
    public AjaxDataResponseMessage delRole(RoleManageModel roleModel) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        try {
            roleManageService.delRole(roleModel);

            ajaxDataResponseMessage.setData(roleModel);
            ajaxDataResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("角色信息操作失败");
        }
        return ajaxDataResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/queryRSassociation"})
    public AjaxTableResponseMessage queryAsso(String datas) {
        AjaxTableResponseMessage ajaxTableResponseMessage = new AjaxTableResponseMessage();
        try {
            Map<String, Object> map = this.objectMapper.readValue(datas, Map.class);
            DataTable table = roleManageService.queryRSasso(map);

            ajaxTableResponseMessage.setDatas(table.getDataRows());
            ajaxTableResponseMessage.setTotalSize((int) table.getTotalRowNum());
            ajaxTableResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxTableResponseMessage.setSuccess(false);
            ajaxTableResponseMessage.setErrorMsg(e.getMessage());
        }
        return ajaxTableResponseMessage;
    }

    @ResponseBody
    @RequestMapping("/save")
    public AjaxDataResponseMessage saveAsso(String datas) throws CustomException {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, Object> map = null;
        int result = -1;
        try {
            if (datas != null) {
                map = this.objectMapper.readValue(datas, Map.class);
            }
            result = roleManageService.saveAsso(map);
            ajaxDataResponseMessage.setData(result);
            if (result > -1)
                ajaxDataResponseMessage.setSuccess(true);
            else
                ajaxDataResponseMessage.setSuccess(false);
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("设置系统失败");
        }
        return ajaxDataResponseMessage;
    }
}
